<template>
  <div>
    <!-- <h3>form组件</h3> -->
    <el-dialog
      :title="status.isAdd ? '商品添加' : '商品编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <el-form :model="form">
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="form.first_cateid" placeholder="请选择" @change="change_first_cate_id">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="form.second_cateid" placeholder="请选择">
            <el-option
              v-for="item in secondCateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品名称" label-width="120px">
          <el-input v-model="form.goodsname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="价格" label-width="120px">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        {{ form }}

        <el-form-item label="市场价格" label-width="120px">
          <el-input v-model="form.market_price" autocomplete="off"></el-input>
        </el-form-item>

        <upload-img @getFile="form.img=$event"></upload-img>

        <el-form-item label="商品规格" label-width="120px">
          <el-select v-model="form.specsid" placeholder="请选择" @change="change_specsid_id">
            <el-option v-for="item in specsList" :key="item.id" :label="item.specsname" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规格属性" label-width="120px">
          <el-select v-model="form.specsattr" placeholder="请选择">
            <el-option :label="item" :value="0" v-for="item in attrsArr" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否新品" label-width="120px">
          <el-radio v-model="form.isnew" :label="1">是</el-radio>
          <el-radio v-model="form.isnew" :label="2">否</el-radio>
        </el-form-item>

        <el-form-item label="是否热卖" label-width="120px">
          <el-radio v-model="form.ishot" :label="1">是</el-radio>
          <el-radio v-model="form.ishot" :label="2">否</el-radio>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>

        <el-form-item label="商品描述" label-width="120px">
          <textarea
            name=""
            id=""
            cols="30"
            rows="10"
            v-model="form.description"
          ></textarea>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isshow = status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { reqspecsadd, reqspecsedit, reqspecsinfo,reqcatelist} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  props: ["status"],
  data() {
    return {
      // 添加接口初始化数据
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 2,
        ishot: 2,
        status: 1,
      },
      //二级分类的列表数据
      secondCateList: [],
      //规格属性的列表
      attrsArr: [],
    };
  },
  methods: {
    ...mapActions({
      reqList: "cate/reqList",
      reqSpecsList:"specs/reqSpecsList"
    }),
    // 选择一级分类重置二级分类
    async change_first_cate_id(){
      // 取出一级分类对应的二级分类赋值给二级分类
      this.form.second_cateid = "";
      let res = await reqcatelist({pid:this.form.first_cateid})
      if(res.data.code==200){
        this.secondCateList=res.data.list?res.data.list:[]
      }
    },
    // 选择商品规格,规格属性重新刷新
    change_specsid_id(){
      console.log(this.form.specsid); 
      // 发请求
      reqspecsinfo({id:this.form.specsid}).then(res=>{
        if(res.data.code==200){
          this.attrsArr=res.data.list[0].attrs?res.data.list[0].attrs:[]//??????????????????????????????????????????????是否有bug
        }
      })
    },
    empty() {
      this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: null,
        description: "",
        specsid: "",
        specsattr: "",
        isnew: "",
        ishot: "",
        status: 1,
      };
    },
    async add() {
      // console.log(this.form);
      let res=await reqgoodsadd(this.form)
        if (res.data.code == 200) {
          console.log("哈哈哈");
          // 弹成功
          // successAlert(res.data.msg);
          // // 清空
          // this.empty();
          // //  弹框消失
          // this.status.isshow = false;
          // // 列表重新渲染
          // this.reqList();
        }
    },

    // 点击编辑之后
    getOne(id) {
      reqspecsinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          console.log(res.data.list[0].attrs);
          this.form = res.data.list[0];
          this.arr = res.data.list[0].attrs.map((item) => {
            return { value: item };
          });
          // 补一个id删除时要用
          this.form.id = id;
        }
      });
    },
    // 点击修改
    update() {
      this.form.attrs = this.arr.map((item) => item.value).join(",");
      reqspecsedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.status.isshow = false;
          this.reqList();
        }
      });
    },
    // 关闭弹框事件
    closed() {
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {
    this.reqList();
    this.reqSpecsList(true);
  },
  computed: {
    ...mapGetters({
      list: "cate/list",
      specsList:"specs/list"
    }),
  },
};
</script>

<style scoped lang="less">
@import "../../../less/index.less";

</style>